/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ajz.web;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ajz.entity.AjzCompanyInfo;
import com.jeesite.modules.ajz.entity.AjzCompanyInfoVO;
import com.jeesite.modules.ajz.service.AjzCompanyInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * 企业信息表Controller
 * @author cww
 * @version 2019-01-11
 */
@Controller
@RequestMapping(value = "${adminPath}/ajz/ajzCompanyInfo")
public class AjzCompanyInfoController extends BaseController {

	@Autowired
	private AjzCompanyInfoService ajzCompanyInfoService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AjzCompanyInfo get(String id, boolean isNewRecord) {
		return ajzCompanyInfoService.get(id, isNewRecord);
	}

	@RequiresPermissions("sys:empUser:view")
	@RequestMapping(value = "index")
	public String index(AjzCompanyInfo ajzCompanyInfo, Model model) {
		return "modules/ajz/ajzCompanyIndex";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ajz:ajzCompanyInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(AjzCompanyInfo ajzCompanyInfo, Model model) {
		model.addAttribute("ajzCompanyInfo", ajzCompanyInfo);
		return "modules/ajz/ajzCompanyInfoList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ajz:ajzCompanyInfo:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AjzCompanyInfo> listData(AjzCompanyInfo ajzCompanyInfo, HttpServletRequest request, HttpServletResponse response) {
		ajzCompanyInfo.setPage(new Page<>(request, response));
		Page<AjzCompanyInfo> page = ajzCompanyInfoService.findPage(ajzCompanyInfo); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ajz:ajzCompanyInfo:view")
	@RequestMapping(value = "form")
	public String form(AjzCompanyInfo ajzCompanyInfo, Model model) {
		model.addAttribute("ajzCompanyInfo", ajzCompanyInfo);
		return "modules/ajz/ajzCompanyInfoForm";
	}

	/**
	 * 保存企业信息表
	 */
	@RequiresPermissions("ajz:ajzCompanyInfo:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AjzCompanyInfo ajzCompanyInfo) {
		ajzCompanyInfoService.save(ajzCompanyInfo);
		return renderResult(Global.TRUE, text("保存企业信息表成功！"));
	}
	
	/**
	 * 删除企业信息表
	 */
	@RequiresPermissions("ajz:ajzCompanyInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AjzCompanyInfo ajzCompanyInfo) {
		ajzCompanyInfoService.delete(ajzCompanyInfo);
		return renderResult(Global.TRUE, text("删除企业信息表成功！"));
	}


//	/**
//	 * 下载导入用户数据模板
//	 */
//	@RequiresPermissions("ajz:ajzCompanyInfo:view")
//	@RequestMapping(value = "importTemplate")
//	public void importTemplate(HttpServletResponse response) {
//		AjzCompanyInfo companyInfo = new AjzCompanyInfo();
//		List<AjzCompanyInfo> list = ListUtils.newArrayList(companyInfo);
//		String fileName = "企业数据模板.xlsx";
//		try(ExcelExport ee = new ExcelExport("企业数据", AjzCompanyInfoVO.class, ExcelField.Type.IMPORT)){
//			ee.setDataList(list).write(response, fileName);
//		}
//	}

	/**
	 * 导入用户数据
	 */
	@ResponseBody
	@RequiresPermissions("ajz:ajzCompanyInfo:edit")
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport) {
		try {
			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			ajzCompanyInfoService.updateVariable();
			String message = ajzCompanyInfoService.importData(file, isUpdateSupport);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}

	/**
	 * 下载导入企业数据模板
	 */
	@RequiresPermissions("ajz:ajzCompanyInfo:view")
	@RequestMapping(value = "importTemplate")
	public void shipinTemplate(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "公司模板.xlsx";
			String realPath = request.getSession().getServletContext().getRealPath("/downLoad/公司模板.xlsx");
			downLoadFile(realPath, response, fileName);
//			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
		}
//		return "redirect:" + adminPath + "/ajz/ajzBaseinfo/list?repage";
	}

	/**
	 * 文件下载
	 *
	 * @param filePath
	 * @param response
	 * @param filename
	 */
	@SuppressWarnings("resource")
	public void downLoadFile(String filePath, HttpServletResponse response, String filename) {
		BufferedOutputStream bufferOut = null;
		BufferedInputStream bufferIn = null;
		try {
			bufferIn = new BufferedInputStream(new FileInputStream(filePath));
			bufferOut = new BufferedOutputStream(response.getOutputStream());
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-disposition", "attachment; filename=" + new String(filename.getBytes("UTF-8"), "iso8859-1"));
			byte[] Byte = new byte[1024];
			int len = 0;
			while (-1 != (len = bufferIn.read(Byte))) {
				bufferOut.write(Byte, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != bufferOut) {
				try {
					bufferOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != bufferIn) {
				try {
					bufferIn.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}


	//测试
	@RequestMapping(value = "test")
	public String test(AjzCompanyInfo ajzCompanyInfo, Model model) {
		return "modules/ajz/ajzCompanyIndex";
	}


}